package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.DoctorEntity;
import com.example.demo.entity.PatientEntity;
import com.example.demo.service.PatientService;
import com.example.demo.tool.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "病人信息相关接口")
public class PatientController {

    @Autowired
    private PatientService patientService;
    @Autowired
    private JavaMailSender javaMailSender;

    @PostMapping("add")
    @ApiOperation(value = "增加",notes = "时间不用增加,后台已经增加了当前时间")
    public ResponseResult add(PatientEntity patientEntity){
        patientEntity.setRegistrationTime(new Date());
        patientService.save(patientEntity);
        return ResponseResult.success();
    }

    @GetMapping("getOne")
    @ApiOperation("根据id获取一条信息")
    public PatientEntity getOne(Integer id){
        return patientService.getById(id);
    }


    @GetMapping("delOne")
    @ApiOperation("根据id删除一条信息")
    public ResponseResult delOne(Integer id){
        patientService.removeById(id);
        return ResponseResult.success();
    }

    @PostMapping("updateOne")
    @ApiOperation("根据id修改一条信息")
    public ResponseResult updateOne(Integer id,PatientEntity patientEntity){
//        patientService.update(new UpdateWrapper<>());
        patientService.updateById(patientEntity);
        return ResponseResult.success();
    }

    @GetMapping("patient/alldata")
    @ApiOperation("分页返回病人信息")
    public ResponseResult listAll(@ApiParam("页码，默认为1") @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                  @ApiParam("每页数量，默认为10") @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<PatientEntity> list = patientService.list(new QueryWrapper<PatientEntity>().orderByDesc("create_time"));
        PageInfo<PatientEntity> page=new PageInfo<>(list);
        return ResponseResult.success(page);
    }


    // 发送简单邮件
    /**
     *
     * @param from
     *            发送方
     * @param to
     *            接收方
     * @param subject
     *            主题
     * @param text
     *            内容
     */
    @PostMapping("/email")
    @ResponseBody
    public void SendEmail(){
        SimpleMailMessage message=new SimpleMailMessage();
//        String[] str={"1121170146@qq.com","1121198894470146@qq.com","1121198894470146@qq.com","2425133932@qq.com"};
        message.setText("上号");
        message.setSubject("test");
        message.setTo("1121170146@qq.com");
        message.setFrom("3112822380@qq.com");
        javaMailSender.send(message);
    }

}
